package cn.yunlang.zhangZhongFei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.yunlang.zhangZhongFei.entity.ExchangeAward;
import cn.yunlang.zhangZhongFei.mapper.ExchangeAwardMapper;
import cn.yunlang.zhangZhongFei.service.ExchangeAwardService;
import cn.yunlang.zhangZhongFei.vo.response.R;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 兑奖记录表 服务实现类
 *
 * @author ·
 * @since 2025-05-27 09:54:49
 */
@Service
public class ExchangeAwardServiceImpl extends ServiceImpl<ExchangeAwardMapper, ExchangeAward>
    implements ExchangeAwardService {

  @Override
  public R<IPage<ExchangeAward>> pageExchangeAward(
      Integer pageNo, Integer pageSize, Long userId, String state, String awardName) {
    if (pageNo == null || pageSize == null) {
      return R.error(400, "分页参数不能为空");
    }
    if (pageNo < 1) {
      pageNo = 1;
    }
    if (pageSize < 1) {
      pageSize = 10;
    }
    LambdaQueryWrapper<ExchangeAward> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(userId != null && userId != 0, ExchangeAward::getUserId, userId);
    wrapper.eq(StringUtils.hasText(state), ExchangeAward::getState, state);
    wrapper.like(StringUtils.hasText(awardName), ExchangeAward::getAwardName, awardName);
    wrapper.orderByDesc(ExchangeAward::getCreateTime);
    Page<ExchangeAward> page = new Page<>(pageNo, pageSize);
    IPage<ExchangeAward> result = this.page(page, wrapper);
    return R.success(result);
  }

  public R<ExchangeAward> getOne(Long exchangeAwardId) {
    if (exchangeAwardId == null) {
      return R.error(400, "ID不能为空");
    }
    ExchangeAward exchangeAward = this.getById(exchangeAwardId);
    if (exchangeAward == null) {
      return R.error(404, "未找到兑奖记录");
    }
    return R.success(exchangeAward);
  }

  public R<String> send(Long exchangeAwardId) {
    if (exchangeAwardId == null) {
      return R.error(400, "ID不能为空");
    }
    boolean update =
        this.update(
            new LambdaUpdateWrapper<ExchangeAward>()
                .set(ExchangeAward::getState, "1")
                .eq(ExchangeAward::getState, "0")
                .eq(ExchangeAward::getId, exchangeAwardId));
    return update ? R.success("成功发货") : R.error(500, "发货失败");
  }

  public R<String> recv(Long exchangeAwardId) {
    if (exchangeAwardId == null) {
      return R.error(400, "ID不能为空");
    }
    boolean update =
        this.update(
            new LambdaUpdateWrapper<ExchangeAward>()
                .set(ExchangeAward::getState, "2")
                .eq(ExchangeAward::getState, "1")
                .eq(ExchangeAward::getId, exchangeAwardId));
    return update ? R.success("成功收货") : R.error(500, "收货失败");
  }

  public R<String> addExchangeAward(ExchangeAward exchangeAward) {
    if (exchangeAward == null
        || exchangeAward.getUserId() == null
        || exchangeAward.getAwardId() == null
        || !StringUtils.hasText(exchangeAward.getAwardName())) {
      return R.error(400, "用户ID、奖品ID、奖品名称不能为空");
    }
    boolean save = this.save(exchangeAward);
    return save ? R.success("新增成功") : R.error(500, "新增失败");
  }

  public R<String> deleteExchangeAward(Long exchangeAwardId) {
    if (exchangeAwardId == null) {
      return R.error(400, "ID不能为空");
    }
    boolean remove = this.removeById(exchangeAwardId);
    return remove ? R.success("删除成功") : R.error(500, "删除失败");
  }

  public R<String> updateExchangeAward(ExchangeAward exchangeAward) {
    if (exchangeAward == null
        || exchangeAward.getId() == null
        || exchangeAward.getUserId() == null
        || exchangeAward.getAwardId() == null
        || !StringUtils.hasText(exchangeAward.getAwardName())) {
      return R.error(400, "ID、用户ID、奖品ID、奖品名称不能为空");
    }
    boolean update = this.updateById(exchangeAward);
    return update ? R.success("修改成功") : R.error(500, "修改失败");
  }
}
